package com.dlsy.ygj.dao.vindicateData.impl;

import java.sql.SQLException;
import java.util.List;

import net.taobaoge.common.framework.dao.impl.BaseDAOImpl;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.util.CollectionUtils;

import com.dlsy.ygj.dao.vindicateData.IFavouredPlanInfoDAO;
import com.dlsy.ygj.entity.FavouredPlanInfo;

public class FavouredPlanInfoDAO extends BaseDAOImpl<FavouredPlanInfo> implements IFavouredPlanInfoDAO{

	@SuppressWarnings("unchecked")
	public List<FavouredPlanInfo> findFavouredPlanInfoByProperty(
			String value, Integer stroreId) {
		Criteria criteria = getSession()
				.createCriteria(FavouredPlanInfo.class);
		criteria.add(Restrictions.and(Restrictions.like(
				"favouredPlanName", "%" + value + "%"), Restrictions.eq(
				"storefrontId", stroreId)));
		List<FavouredPlanInfo> favouredPlanInfos = criteria.list();
		if (favouredPlanInfos != null && favouredPlanInfos.size() > 0) {
			return favouredPlanInfos;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<FavouredPlanInfo> checkExist(final Integer favouredPlanId,
			final String value, final Integer stroreId) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public List<FavouredPlanInfo> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = getSession().createCriteria(
						FavouredPlanInfo.class);
				criteria.add(Restrictions.and(Restrictions.and(Restrictions
								.eq("favouredPlanName", value), Restrictions
						.not(Restrictions
								.eq("favouredPlanId", favouredPlanId))),
						Restrictions.eq("storefrontId", stroreId)));
				List<FavouredPlanInfo> favouredPlanInfos = criteria.list();
				if (favouredPlanInfos != null && favouredPlanInfos.size() > 0) {
					return favouredPlanInfos;
				}
				return null;
			}
		});
	}

	public List<FavouredPlanInfo> findAll(Integer stroreId) {
		try {
			List<FavouredPlanInfo> favouredPlanInfos = findByProperty(
					FavouredPlanInfo.class, "storefrontId", stroreId);
			if (!CollectionUtils.isEmpty(favouredPlanInfos)) {
				return favouredPlanInfos;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<FavouredPlanInfo> checkExist(final String value,
			final Integer stroreId) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public List<FavouredPlanInfo> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = getSession().createCriteria(
						FavouredPlanInfo.class);
				criteria.add(Restrictions.and( Restrictions.eq(
						"favouredPlanName", value), Restrictions.eq(
						"storefrontId", stroreId)));
				List<FavouredPlanInfo> favouredPlanInfos = criteria.list();
				if (favouredPlanInfos != null && favouredPlanInfos.size() > 0) {
					return favouredPlanInfos;
				}
				return null;
			}
		});
	}
	public Integer saveAndReturn(FavouredPlanInfo favouredPlanInfo){
		return (Integer)getHibernateTemplate().save(favouredPlanInfo);
	}
}
